package com.lib.controller.databoard;

import java.io.File;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class DataBoardDeleteController implements ApplicationContextAware {
	private WebApplicationContext context = null;
	@Autowired
	private com.lib.model.service.IDataBoardService dataBoardSerivce;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.context = (WebApplicationContext) applicationContext;
	}

	@RequestMapping("/deleteboard.do")
	public ModelAndView deleteBoard(HttpServletRequest request,
			HttpServletResponse response) {

		ModelAndView mav = new ModelAndView();
		String databoard_num = request.getParameter("databoard_num");
		String fileName = request.getParameter("fileName");

		String root = context.getServletContext().getRealPath("/data/");

		File file = new File(root, fileName);

		file.delete();
		dataBoardSerivce.deleteData(databoard_num);
		mav.setViewName("redirect:/datalist.do");

		return mav;

	}

}
